Frontmatter

Introduction

This book will focus on teaching you the fundamentals of web development.

Web development is a powerful tool that can help you in many endeavors—whether it’s building a startup that can change the world, getting a job to provide for your family, making a web app as a creative expression, or using web development as a secret weapon to increase your effectiveness in your current career, I think that you’ll be glad you invested the time into learning this valuable skill.

We’re going to learn web development using what I believe are some of the best tools available: Elixir (a blazing-fast programming language with friendly syntax), Phoenix (a modern web framework that combines functional programming with the best of Ruby on Rails), and Ecto (a maintainable system for interacting with the database). (More on that later in this introduction)

What are we building?

We’ll be taking a hands-on approach to learning, which means you’ll be practicing each concept quickly after you learn it, applying it in a real application when possible. In addition to the code given in each chapter (which you should type into your computer and run), there will also be exercises, which I highly recommend you do—if you want to remember what you’re learning.

Throughout this book we’re going to be building the Star Tracker app, which boldly goes where no resource exchange app has gone before.

Note: this is an early version of the app. There will be some stylistic and usability improvements made before we get to that part of the tutorial.

Insert picture of finished app here

We’ll be creating a way to buy and sell resources (mostly the elements that make up stars, hence the name) on a centralized market. You click a button, you buy and sell a resource. We’ll use LiveView to automatically show the user when someone else makes a trade - without a page refresh, and without us writing a single line of Javascript.

It’s not super fancy, but it gives us many opportunities to solidify the core concepts you’ll need to start creating your own Phoenix apps (and understand the more advanced Elixir and Phoenix books, should you choose to read them).

Why this book, and not Programming Phoenix or another book?

There are lots of great resources for intermediate and advanced developers to learn Elixir and Phoenix, but relatively little for beginners. In addition, the resources that do exist tend to rush past the core web framework features in order to get to the cool advanced features that set the framework apart, like Channels and OTP.

This book aims to be your first Elixir and Phoenix book (and maybe even your first web development book), but not your last. We’ll go into the basics of the language and the framework, giving you a solid foundation so you can crank out simple web apps and be prepared to read and understand the books written on more advanced topics.

That said, this is not meant to be your first introduction to programming.

Prerequisites

While I don’t expect you to have familiarity with Elixir or web programming, I do expect you to know how to put together a simple program in some language. If you don’t meet that standard, here are some good (free) resources:

Why Phoenix and Elixir?

This section uses advanced terminology in order to compare Phoenix and Elixir to other tools. If you don’t understand something here that’s okay—just read to get the general gist of it. There will be a summary at the end.

The current king of the backend Web Framework world is Ruby on Rails—it’s what is taught at hundreds of Coder Bootcamps and used at thousands of startups (as well as some large companies). However, many of the leading lights of the Ruby and Rails worlds have moved on, and a significant number of them have moved to Elixir and Phoenix. Why is that?

Twitter joke about Rails and Phoenix

The answer is that Elixir combines the power, speed, and reliability of the Erlang VM with the beauty and expressiveness of Ruby syntax. In one stroke it fixes the biggest complaint about each language- speed for Ruby and syntax for Erlang.

The Elixir ecosystem has also learned a lot from both languages. From Ruby it takes many of the most popular tools and combines them into one tool with a consistent interface: Mix. From Erlang it takes… the entire Erlang ecosystem; you can use any Erlang package you want in Elixir (just be prepared to look at some not-as-beautiful code).

Phoenix itself has benefited greatly from seeing what Rails has done. It’s taken what worked well (convention over configuration, REST, the Rails MVC style, friendly build tooling, etc.) while doing some much-needed modernization. Some of the modernizations are made available simply by escaping the inertia of the Rails codebase and creator DHH’s opinions. Others are unlocked due to the increased power and features of Elixir as compared to Ruby.

So here are the biggest advantages of Phoenix and Elixir, in fancy listicle form:

  • SPEED

Elixir is an order of magnitude (~10X) faster than Ruby right out of the box. This not only gets you faster response times and lets you handle more requests per box, but it eliminates a whole class of performance hacks and opens the door for doing things that would not be wise in Ruby.

  • Reliability

Elixir is built on the Erlang VM, which has created systems with “nine nines” of reliability- that is, it was working 99.9999999% of the time. This reliability isn’t necessarily because individual parts crash less often, but because it can recover seamlessly from crashes- a “self healing” network of processes. We won’t get into this mechanism in detail in this book.

  • Real-time features

Channels/Sockets that are just as easy as in Node (the current Sockets king), but in Elixir they’re more performant. This means that “real-time” communication between server and client is a breeze compared to other languages and frameworks.

  • Functional Programming Style

This can occasionally cause frustration for those coming from an Object-Oriented language (such as Ruby), but once you become acclimated it means fewer bugs and more readable code. You also get really cool features like Pattern Matching and piping that are not available in Object-Oriented languages.

  • LiveView

Tired of sprinkling Javascript all over your app? LiveView lets you do form validations, auto-completes, complex interactions, and even real-time gaming, all from the server. But it doesn’t just eliminate the burden of managing two separate frameworks… it’s going to do for real-time apps what Rails did for request/response CRUD apps.


Technobabble: When to use Phoenix vs Other web frameworks?

Phoenix is still a newer technology, but it’s well past 1.0 and stable enough to run production apps (and the underlying technologies—Elixir and Erlang—are rock solid).

If you’re starting a new project, choose Phoenix! But if you have a legacy project then there are more tradeoffs to consider.

I’d say that if you are having performance problems which can’t be solved with basic solutions (such as fixing n+1 database queries) then it may be worth it to switch to Phoenix. High availability requirements are another good reason. It also may be worth it if your project involves lots of concurrency or sockets- stuff that most languages and frameworks do poorly, but Elixir and Phoenix do really really well.

Another huge reason to switch to Phoenix is if you want real-time interactive apps. Phoenix LiveView lets you have that without having to manage two frameworks + an API layer, like you would if you used, for example, Rails and React. It’s also fantastic for dashboards and other apps that need up-to-date information from the server.

If you’re investigating Phoenix, then it’s likely that you fall into one of those camps. However, if you’re happy with your current tech, or if you’re unhappy for different reasons, consider whether the benefits of Phoenix will be worth the cost of a rewrite.


Why Phoenix? (for beginners)

If you’re a beginner that didn’t quite understand all of the last section, here’s the tl;dr on why Elixir/Phoenix/Ecto can be a better choice than Ruby/Ruby on Rails/ActiveRecord or other popular web programming tools:

  • Phoenix is an order of magnitude faster (due to Elixir’s use of the Erlang VM)
  • Your Phoenix code is significantly less likely to descend into spaghetti code (due both to Elixir’s functional programming and Phoenix and Ecto’s learning many lessons from how web apps can go wrong)
  • LiveView means you don’t have to learn React
  • You’ll be prepared to take advantage of Elixir’s advanced features when their use becomes necessary

Conclusion

I hope you’re as excited as I am to begin! Turn the page and we’ll start installing Elixir.



Buy the Ebook

Subscribe to the mailing list